package com.example.petshopping.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.PetProduct;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.snackbar.Snackbar;

import java.util.List;

public class PetProductAdapter extends RecyclerView.Adapter<PetProductAdapter.ProductViewHolder> {

    private final Context context;
    private final List<PetProduct> productList;

    public PetProductAdapter(Context context, List<PetProduct> productList) {
        this.context = context;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_pet_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        PetProduct product = productList.get(position);
        
        holder.titleView.setText(product.getTitle());
        holder.priceView.setText("¥" + product.getPrice());
        holder.tagView.setText(product.getTag());
        holder.timeView.setText(product.getAuctionEndTime());
        holder.sellerView.setText(product.getSellerName());
        holder.imageView.setImageResource(product.getImageResource());
        
        // 设置收藏按钮状态
        updateFavoriteButton(holder.favoriteButton, product.isFavorite());
        
        // 收藏按钮点击事件
        holder.favoriteButton.setOnClickListener(v -> {
            boolean newState = !product.isFavorite();
            product.setFavorite(newState);
            updateFavoriteButton(holder.favoriteButton, newState);
        });
        
        // 删除按钮点击事件
        holder.deleteButton.setOnClickListener(v -> {
            int adapterPosition = holder.getAdapterPosition();
            if (adapterPosition != RecyclerView.NO_POSITION) {
                productList.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
                Snackbar.make(v, "已移除该商品", Snackbar.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }
    
    private void updateFavoriteButton(ImageView favoriteButton, boolean isFavorite) {
        if (isFavorite) {
            favoriteButton.setImageResource(R.drawable.ic_heart_filled);
        } else {
            favoriteButton.setImageResource(R.drawable.ic_heart_outline);
        }
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        final ImageView imageView;
        final TextView titleView;
        final TextView priceView;
        final TextView tagView;
        final TextView timeView;
        final TextView sellerView;
        final ImageView favoriteButton;
        final ImageView deleteButton;
        final MaterialCardView cardView;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (MaterialCardView) itemView;
            imageView = itemView.findViewById(R.id.product_image);
            titleView = itemView.findViewById(R.id.product_title);
            priceView = itemView.findViewById(R.id.product_price);
            tagView = itemView.findViewById(R.id.product_tag);
            timeView = itemView.findViewById(R.id.product_time);
            sellerView = itemView.findViewById(R.id.seller_name);
            favoriteButton = itemView.findViewById(R.id.favorite_button);
            deleteButton = itemView.findViewById(R.id.delete_button);
        }
    }
} 